<template>
  <div>
    <el-button
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      type="primary"
      @click="toAddSpuInfo"
      >添加SPU</el-button
    >
    <el-table border :data="spuList" style="margin: 20px 0" v-loading="loading">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <el-button
            size="small"
            title="添加SKU"
            :icon="Plus"
            @click="toSkuForm(row)"
          ></el-button>
          <el-button
            size="small"
            title="修改SPU"
            :icon="Edit"
            @click="toEditSpuInfo(row)"
          ></el-button>
          <el-button
            size="small"
            title="查看SKU列表"
            :icon="InfoFilled"
            @click="toCheckSkuList(row.id)"
          ></el-button>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="真的确定要删除此条数据么?"
            @confirm="deleteSpuInfo(row.id)"
          >
            <template #reference>
              <el-button
                size="small"
                title="删除SPU"
                :icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 弹出框列表 -->
    <el-dialog v-model="dialogSkuTableVisible" title="查看Sku数据列表">
      <el-table :data="skuList" v-loading="loading">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格(元)" prop="price" />
        <el-table-column label="重量(KG)" prop="weight" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <el-image
              :src="row.skuDefaultImg"
              style="width: 100px; height: 100px"
            ></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: "SpuList",
}
</script>
<script lang="ts" setup>
import { ref, watch } from "vue"
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue"
import { reqGetSpuListData, reqDeleteSpuInfo } from "@/api/product/spu"
import { useCategoryListStore } from "@/stores/categoryList"
import { ElMessage } from "element-plus"
import { reqGetSkuListData } from "@/api/product/sku"
const emits = defineEmits(["changeScene", "getSpuInfo"]) // 接收自定义事件
const loading = ref<boolean>(false)
const categoryStore = useCategoryListStore()
const spuList = ref([])
const currentPage = ref<number>(1)
const pageSize = ref<number>(5)
const total = ref<number>()
const small = ref<boolean>(false)
const background = ref<boolean>(false)
const disabled = ref<boolean>(false)
// 分页相关的两个事件
// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  getSpuListData()
}

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getSpuListData()
}

// 获取列表数据
// 1.监视三级导航Id
const getSpuListData = async () => {
  loading.value = true
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id
  )
  // console.log(result)
  spuList.value = result.records
  total.value = result.total

  loading.value = false
}
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      // 清空列表
      spuList.value = []
      return
    }
    getSpuListData()
  },
  { immediate: true }
)

// 5. 删除spu
const deleteSpuInfo = async (id: number) => {
  await reqDeleteSpuInfo(id)
  ElMessage.success("删除spu成功...")

  // 重新刷新页面
  getSpuListData()
}
// 更新spu之数据回显
const toEditSpuInfo = (row) => {
  emits("changeScene", 2)
  emits("getSpuInfo", {
    ...row,
    spuImageList: [], // 把缺少的两项，手动补上
    spuSaleAttrList: [],
  })
}

const toAddSpuInfo = () => {
  emits("changeScene", 2)
  emits("getSpuInfo", {})
}
// 去添加skuInfo
const toSkuForm = (row) => {
  emits("changeScene", 3)
  //实现数据的传递
  emits("getSpuInfo", row)
}
// 9. 查看skuList
const toCheckSkuList = async (id: number) => {
  dialogSkuTableVisible.value = true
  loading.value = true
  skuList.value = await reqGetSkuListData(id)

  // 去掉loading
  loading.value = false
}
</script>

<style></style>
